﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;

namespace CodeBuilder.DevTools
{
    public partial class HostForm : UserControl
    {
        public HostForm()
        {
            InitializeComponent();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.LoadHosts();
        }

        private void LoadHosts()
        {
            string hostsFile = this.GetHostsFile();
            string text = ReadFile(hostsFile);
            this.txt4HostPath.Text = hostsFile;
            this.txt4HostContent.Clear();
            this.txt4HostContent.AppendText(text);
            this.txt4HostContent.ScrollToCaret();
        }

        private string GetHostsFile()
        {
            return (Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\drivers\etc\hosts");
        }


        public static string ReadFile(string file)
        {
            string str2;
            string str = "";
            using (FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                StreamReader reader = new StreamReader(stream, Encoding.Default);
                try
                {
                    str2 = reader.ReadToEnd();
                }
                catch
                {
                    str2 = str;
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                }
            }
            return str2;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.txt4HostContent.AppendText("\r\n" + this.txtIP.Text + " " + this.txtDomain.Text);

        }

        /// <summary>
        /// 写入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string text = this.txt4HostContent.Text;
                string hostsFile = this.GetHostsFile();
                WriteFile(hostsFile, text);
                MessageBox.Show("文件： " + hostsFile + " 写入成功");
            }
            catch (Exception exception)
            {
                MessageBox.Show("错误信息： " + exception);
            }
        }

        public static string WriteFile(string file, string fileContent)
        {
            string str;
            FileInfo info = new FileInfo(file);
            if (!Directory.Exists(info.DirectoryName))
            {
                Directory.CreateDirectory(info.DirectoryName);
            }
            FileStream stream = new FileStream(file, FileMode.Create, FileAccess.Write);
            StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
            try
            {
                try
                {
                    writer.Write(fileContent);
                    str = fileContent;
                }
                catch (Exception exception)
                {
                    throw new FileNotFoundException(exception.ToString());
                }
            }
            finally
            {
                writer.Flush();
                stream.Flush();
                writer.Close();
                stream.Close();
            }
            return str;
        }

        /// <summary>
        /// backup
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string hostsFile = this.GetHostsFile();
            string destFileName = this.GetCurrentPath() + @"\hosts";
            System.IO.File.Copy(hostsFile, destFileName, true);
            MessageBox.Show("文件： " + hostsFile + " 备份成功");
        }

        /// <summary>
        /// clear
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            this.txt4HostContent.Clear();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string hostsFile = this.GetHostsFile();
            string path = this.GetCurrentPath() + @"\hosts";
            try
            {
                if (System.IO.File.Exists(path))
                {
                    System.IO.File.Copy(path, hostsFile, true);
                    MessageBox.Show("文件： " + path + " 还原成功");
                }
                else
                {
                    MessageBox.Show("备份文件： " + path + " 不存在");
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        private string GetCurrentPath()
        {
            return Environment.CurrentDirectory;
        }

    }
}
